package cn.xej.health.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @author xej
 * 跨域配置
 */
@Configuration
public class GlobalCorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.addAllowedHeader("*"); //支持请求暴露的字段
        configuration.addAllowedMethod("*"); // get、post、put、delete方法
        configuration.addAllowedOrigin("*"); // 支持哪些来源的请求跨域
        configuration.setAllowCredentials(true);  //表示允许携带cookie

        source.registerCorsConfiguration("/**",configuration);

        return new CorsWebFilter(source);
    }
}
